Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

relative-url

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

relative-url

convert a partial url into an absolute url from your current location.

  • 1.0.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
356
increased by27.14%
Maintainers
1
Weekly downloads
 
Created
Source

relative-url

convert a partial url into an absolute url from your current location.

In the WebSocket api, it expects a fully qualified url, but this is awkward because we want to develop on localhost, and then deploy to a server with a proper domain, with a different port.

We want to create a WebSocket at a path, and get whatever domain we are on.

This module generalized wsurl

api: rurl(url, location, protocolMap, defaultProtocol)

url is your partial url, it will be described below. location should be your is window.location or the output of require('url').parse(absoluteUrl)

if you want to change the protocol, protocol map is a object mapping current to desired protocol.

for websockets, you'll probably want: {http: 'ws', https: 'wss'}

defaultProtocol is what to set the protocol to otherwise. for example, 'ws' if defaultProtocol is not provided the output protocol will the the same as location.protocol

examples

for the following location:

var location = {
  protocol: 'http',
  host: 'server.com',
  pathname '/foo'
} //OR get this from window.location

var protocolMap = {http: 'ws', https: 'wss'}

var defaultProtocol : 'ws'

var rurl = require('relative-url')
function relative (url) {
  return rurl(url, location, protocolMap, defaultProtocol) 
}


and the above mentioned settings (suitable for websockets) output will be as following.


relative('/')            // => ws://server.com/
relative('/bar')         // => ws://server.com/bar
relative('//client.com') // => ws://client.com/
relative('//:9999')      // => ws://server.com:9999/
relative('?q=search'     // => ws://server.com/foo?q=search

note that // denotes the start of the host. if you want to set the port without changing the domain, you must still use // otherwise the url module will think it's a path. I considered detecting that, but then i saw this comment and got scared of learning too much about urls.

License

MIT

FAQs

Package last updated on 08 Jun 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc